import { createRouter, createWebHistory } from 'vue-router'

// Import views (lazy loading)
const Home = () => import('../views/Home.vue')
const BookLibrary = () => import('../views/BookLibrary.vue')
const BookDetail = () => import('../views/BookDetail.vue')
const AIChat = () => import('../views/AIChat.vue')
const ImageGeneration = () => import('../views/ImageGeneration.vue')
const Explore = () => import('../views/Explore.vue')
const ExploreNode = () => import('../views/ExploreNode.vue')
const Profile = () => import('../views/Profile.vue')
const About = () => import('../views/About.vue')
const ApiTest = () => import('../views/ApiTest.vue')

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    meta: {
      title: '首页',
      description: 'AI古籍寻踪沉浸式探索平台'
    }
  },
  // Legacy exploration route removed; if deep links exist, consider enabling this redirect:
  // { path: '/exploration', redirect: '/explore' },
  {
    path: '/explore',
    name: 'Explore',
    component: Explore,
    meta: { title: '探索模式', description: '探索模式节点与任务', fullWidth: true }
  },
  {
    path: '/explore/node/:id',
    name: 'ExploreNode',
    component: ExploreNode,
    meta: { title: '节点详情', description: '探索模式节点详情', fullWidth: false }
  },
  {
    path: '/explore/achievements',
    name: 'ExploreAchievements',
    component: () => import('../views/ExploreAchievements.vue'),
    meta: { title: '成就徽章', description: '探索模式成就', fullWidth: true }
  },
  {
    path: '/explore/graph',
    name: 'ExploreGraph',
    component: () => import('../views/ExploreGraph.vue'),
    meta: { title: '探索图谱', description: '节点关系图谱', fullWidth: true }
  },
  {
    path: '/library',
    name: 'BookLibrary',
    component: BookLibrary,
    meta: {
      title: '典籍库',
      description: '浏览和选择古典典籍',
      fullWidth: true
    }
  },
  {
    path: '/library/:bookId',
    name: 'BookDetail',
    component: BookDetail,
    meta: {
      title: '典籍详情',
      description: '古籍内容与章节阅读'
    }
  },
  {
    path: '/chat',
    name: 'AIChat',
    component: AIChat,
    meta: {
      title: 'AI对话',
      description: '与AI对话解读古籍内容',
      fullWidth: true
    }
  },
  
  {
    path: '/image',
    name: 'ImageGeneration',
    component: ImageGeneration,
    meta: {
      title: 'AI绘图',
      description: 'AI文生图功能',
      fullWidth: true
    }
  },
  
  {
    path: '/profile',
    name: 'Profile',
    component: Profile,
    meta: {
      title: '个人中心',
      description: '用户个人信息和探索记录'
    }
  },
  
  {
    path: '/debug-auth',
    name: 'DebugAuth',
    component: () => import('../views/DebugAuth.vue'),
    meta: {
      title: '认证诊断',
      description: '调试认证状态'
    }
  },
  {
    path: '/about',
    name: 'About',
    component: About,
    meta: {
      title: '关于我们',
      description: '了解AncientSeeker项目'
    }
  },
  {
    path: '/api-test',
    name: 'ApiTest',
    component: ApiTest,
    meta: {
      title: 'API测试',
      description: '测试后端所有API功能'
    }
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/'
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})

// Navigation guards
router.beforeEach((to, from, next) => {
  // Update document title
  document.title = to.meta.title 
    ? `${to.meta.title} - AncientSeeker` 
    : 'AncientSeeker - 古籍寻踪'
  
  next()
})

export default router